import { getChannelList, getCompanyProductInfo, updateSaasCompany } from '@/api/company'
import { Component, Emit, Prop, Watch } from 'vue-facing-decorator'
import Render from '@/render/Render'
import renderModal, {
    getModelValue,
    initModelValue,
    type FormModalDto
} from '@/components/modal/Modal'
import toNative from '@/utils/toNative'
import { getReceiptData } from '@/utils/util'
import type { CompanyDetailDto, CompanyInfoDto, CompanyProductInfoDto } from '@/api/company/types'
import { message } from 'ant-design-vue'
import { closeProduct } from '@/api/product'

@Component
export class DialogCheckInfoView extends Render {
    @Prop({ default: false }) show!: boolean
    @Prop() companyId!: number

    companyProductInfo = {} as CompanyProductInfoDto

    @Emit('update:show')
    showChange(val: boolean) {
        return val
    }

    @Emit('refresh')
    handleRefresh() {}

    @Watch('show', { immediate: true })
    async handleWatchShow() {
        this.formConfig.show = this.show
        if (this.show) {
            this.companyProductInfo = await this.getData()
            initModelValue(this.formConfig, this.companyProductInfo)  
            const productList = ['isOpenPaySalary', 'isOpenWesure', 'isOpenEleSign']
            for (const item of productList) {
                this.formConfig.items[item].readonly = !this.companyProductInfo[item as keyof typeof this.companyProductInfo]
            }
        }
    }

    formConfig = this.initFormConfig() as FormModalDto

    initFormConfig() {
        return {
            title: '产品管理',
            width: '450px',
            theme: 'dark',
            show: false,
            items: {
                isOpenPaySalary: {
                    name: '发薪专户',
                    element: 'switch',
                    params: {
                        activeValue: 1,
                        inactiveValue: 0
                    }
                },
                // isOpenPeriod: {
                //     name: '长期保',
                //     element: 'switch',
                //     params: {
                // activeValue: 1,
                // inactiveValue: 0
                //     }
                // },
                isOpenWesure: {
                    name: '日日保',
                    element: 'switch',
                    params: {
                        activeValue: 1,
                        inactiveValue: 0
                    }
                },
                // isOpenGroup: {
                //     name: '团意保',
                //     element: 'switch',
                //     params: {
                // activeValue: 1,
                // inactiveValue: 0
                //     }
                // },
                isOpenEleSign: {
                    name: '电子合同',
                    element: 'switch',
                    params: {
                        activeValue: 1,
                        inactiveValue: 0
                    }
                },
            },
            complete: async () => {
                const data = getModelValue(this.formConfig)

                await closeProduct({
                    companyId: this.companyId,
                    ...data
                })

                message.success('操作成功')
                this.handleRefresh()
                this.formConfig.show = false
                this.showChange(false)
            },
            cancel: () => {
                this.formConfig.show = false
                this.showChange(false)
            }
        } as FormModalDto
    }

    async getData() {
        const res  = await getCompanyProductInfo({
            companyId: this.companyId
        })
        return res
    }

    render() {
        return <div>{renderModal(this.formConfig)}</div>
    }
}

export default toNative<{ companyId: number }, { refresh: () => void }>(DialogCheckInfoView)
